<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>外卖</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        #as {
            position: absolute;
            top: 64px;
            right: -143px;
            color: red;
        }
    </style>
</head>
<body id="body">
<div>
    <form action="" method="post">
        <label for="account">
            <p>
                用户名/邮箱：<input type="text" id="account" name="account" placeholder="请输入用户名" required>
                <span id="as"></span>
            </p>
        </label>
        <label for="password">
            <p>
                密码：<input type="password" id="password" name="password" placeholder="请输入密码" required><br>
            </p>
        </label>
        <label for="submit">
            <p>
                <input type="button" id="submit" value="登录">
                <span id="message"></span>
            </p>
        </label>
    </form>
    <a href="Register.jsp">注册</a><br>
    <a href="HomePage.jsp">返回</a>
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    $(function () {
        $(":button").on("click", function () {
            $.ajax({
                url: "http://localhost:8080/user.do",
                type: "post",
                data: {
                    account: $("#account").val(),
                    password: $("#password").val(),
                    method: 'login'
                },
                dataType: "text",
                success: function (data) {
                    if (data === 'error') {
                        $('#message').text('用户名或密码错误！');
                    } else {
                        let parse = JSON.parse(data);
                        if (parse.identity === 1) {
                            window.location.href = "/User/UserPage.jsp";
                        }
                        if (parse.identity === 2) {
                            window.location.href = "/Seller/SellerPage.jsp";
                        }
                        if (parse.identity === 3) {
                            window.location.href = "/Manager/ManagerPage.jsp";
                        }
                    }
                }
            });
        });
    });
</script>
</body>
</html>
